/**
 * Copyright © 2024 650 Industries.
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import type { ConfigAPI, PluginObj, NodePath, types as t } from '@babel/core';

export default function minifyPlatformSelectPlugin({
  types: t,
}: ConfigAPI & typeof import('@babel/core')): PluginObj {
  function isPlatformSelect(path: NodePath<t.CallExpression>): boolean {
    return (
      t.isMemberExpression(path.node.callee) &&
      t.isIdentifier(path.node.callee.object) &&
      t.isIdentifier(path.node.callee.property) &&
      path.node.callee.object.name === 'Platform' &&
      path.node.callee.property.name === 'select' &&
      t.isObjectExpression(path.node.arguments[0])
    );
  }

  function findProperty(objectExpression: t.ObjectExpression, key: string, fallback: () => any) {
    let value = null;
    for (const p of objectExpression.properties) {
      if (!t.isObjectProperty(p) && !t.isObjectMethod(p)) {
        continue;
      }
      if (
        (t.isIdentifier(p.key) && p.key.name === key) ||
        (t.isStringLiteral(p.key) && p.key.value === key)
      ) {
        if (t.isObjectProperty(p)) {
          value = p.value;
          break;
        } else if (t.isObjectMethod(p)) {
          value = t.toExpression(p);
          break;
        }
      }
    }
    return value ?? fallback();
  }

  function hasStaticProperties(objectExpression: t.ObjectExpression) {
    return objectExpression.properties.every((p) => {
      if (('computed' in p && p.computed) || t.isSpreadElement(p)) {
        return false;
      }
      if (t.isObjectMethod(p) && p.kind !== 'method') {
        return false;
      }
      return t.isIdentifier(p.key) || t.isStringLiteral(p.key);
    });
  }

  return {
    visitor: {
      CallExpression(path, state) {
        const node = path.node;
        const arg = node.arguments[0];
        const opts = state.opts as { platform: string };
        if (isPlatformSelect(path) && t.isObjectExpression(arg)) {
          if (hasStaticProperties(arg)) {
            let fallback: any;
            if (opts.platform === 'web') {
              fallback = () => findProperty(arg, 'default', () => t.identifier('undefined'));
            } else {
              fallback = () =>
                findProperty(arg, 'native', () =>
                  findProperty(arg, 'default', () => t.identifier('undefined'))
                );
            }
            path.replaceWith(findProperty(arg, opts.platform, fallback));
          }
        }
      },
    },
  };
}
